#include "cetele.h"

Cetele::Cetele(int x,int y,int w,int h,const char *p) : Fl_Group(x,y,w,h,p)
{
    color(FL_SELECTION_COLOR);
    compid=1;
    nodeid=100;
    bagid=50;
    ciz=NULL;
    Fl::add_timeout(0.10,sil_CB,this);
    Fl::add_timeout(0.10,yazdir_CB,this);
    end();
    buffer=new Fl_Text_Buffer();
}

Cetele::~Cetele()
{
    for(int i=0;i<compTut.size();i++)
    {
        delete compTut[i];
    }
    compTut.clear();
    for(int i=0;i<nodeTut.size();i++)
    {
        delete nodeTut[i];
    }
    nodeTut.clear();
    for(int i=0;i<metinTut.size();i++)
    {
        delete metinTut[i];
    }
    metinTut.clear();
    for(int i=0;i<cizgiTut.size();i++)
    {
        delete cizgiTut[i];
    }
    cizgiTut.clear();
}

void Cetele::draw()
{
    for(int i=y();i<=h()+y();i+=10)
    {
        fl_line(0,i,w(),i);
        if(i%20==0)
            fl_color(fl_rgb_color(50,50,50));
        else
            fl_color(fl_rgb_color(150,150,150));
    }
    fl_line_style(FL_DOT,1,"");

    for(int i=x();i<w()+x();i+=10)
    {
        fl_line(i,y()+10,i,h()+60);
        if(i%20==0)
            fl_color(fl_rgb_color(100,100,100));
        else
            fl_color(fl_rgb_color(150,150,150));
    }
     fl_line_style(FL_DOT,1,"");
     Fl_Group::draw();
}
int Cetele::handle(int e)
{
    Form *p=dynamic_cast<Form *>(parent()->parent());
    switch(e)
    {
        case FL_PUSH:
            if(Fl::event_button3())
            {
                if(p->eleman()==0)
                {
                    nodeid++;
                    Node *nodes=new Node(Fl::event_x(),Fl::event_y(),nodeid);
                    add(nodes);
                    nodeTut.push_back(nodes);
                    p->redraw();
                }
                else if(p->eleman()==1)
                {
                    component *c=new component(Fl::event_x(),Fl::event_y(),compid,"lamp");
                    compid+=2;
                    add(c);
                    compTut.push_back(c);
                    p->redraw();
                }
                else if(p->eleman()==2)
                {
                    component *c=new component(Fl::event_x(),Fl::event_y(),compid,"resistance");
                    compid+=2;
                    add(c);
                    compTut.push_back(c);
                    p->redraw();
                }
                else if(p->eleman()==3)
                {
                    component *c=new component(Fl::event_x(),Fl::event_y(),compid,"switch");
                    compid+=2;
                    add(c);
                    compTut.push_back(c);
                    p->redraw();
                }
                else if(p->eleman()==4)
                {
                    component *c=new component(Fl::event_x(),Fl::event_y(),compid,"diode");
                    compid+=2;
                    add(c);
                    compTut.push_back(c);
                    p->redraw();
                }
                else if(p->eleman()==5)
                {
                    component *c=new component(Fl::event_x(),Fl::event_y(),compid,"battery");
                    compid+=2;
                    add(c);
                    compTut.push_back(c);
                    p->redraw();
                }
                else if(p->eleman()==6)
                {
                    eContainer *m=new eContainer(Fl::event_x(),Fl::event_y(),100,100,true,true);
                    add(m);
                    metinTut.push_back(m);
                    p->redraw();
                }
            }
            else if(Fl::event_button1() && Fl::event_shift())
            {
                nodeid++;
                Node *nodes=new Node(Fl::event_x(),Fl::event_y(),nodeid);
                add(nodes);
                nodeTut.push_back(nodes);
                p->redraw();
            }
            break;
        case FL_KEYDOWN:
            if(Fl::event_ctrl() && Fl::event_key()==FL_Delete)
            {
                this->clear();
                p->remove(p->prop());
                nodeTut.clear();
                metinTut.clear();
                compTut.clear();
                tumnodetut.clear();
            }
            break;
    }
    tumnodetut.clear();
    tumnodetut=nodeTut;
    for(int i=0;i<compTut.size();i++)
    {
        tumnodetut.push_back(compTut[i]->saggonder());
        tumnodetut.push_back(compTut[i]->solgonder());
    }
    return Fl_Group::handle(e);
}

void Cetele::sil_CB(void *userdata)
{
    Cetele *c=(Cetele *)userdata;
    c->sil_CB2();
}
void Cetele::sil_CB2()
{
    Form *p=(Form*)parent()->parent();
    std::vector<Node *>::iterator nodit;
    Node *n=NULL;
    for(int i=0;i<nodeTut.size();i++)
    {
        n=nodeTut[i];
        if(n->deletesil())
        {
            Cizik *cizyedek=NULL;
            for(int j=0;j<n->gelencizgitut.size();j++)
            {
                cizyedek=n->gelencizgitut[j];
                cizyedek->nodeVer1()->gidensil(cizyedek);
                remove(cizyedek);
                delete cizyedek;
            }
            for(int j=0;j<n->gidencizgitut.size();j++)
            {
                cizyedek=n->gidencizgitut[j];
                cizyedek->nodeVer2()->gelensil(cizyedek);
                remove(cizyedek);
                delete cizyedek;
            }
            nodit=nodeTut.begin()+i;
            nodeTut.erase(nodit);
            p->remove(p->prop());
            delete p->prop();
            remove(n);
            delete n;
        }
    }
    std::vector<component *>::iterator compit;
    component *c=NULL;
    for(int i=0;i<compTut.size();i++)
    {
        c=compTut[i];
        if(c->deletesil())
        {
            Cizik *cizyedek=NULL;
            for(int j=0;j<c->saggonder()->gelencizgitut.size();j++)
            {
                cizyedek=c->saggonder()->gelencizgitut[j];
                cizyedek->nodeVer1()->gidensil(cizyedek);
                remove(cizyedek);
                delete cizyedek;
            }

            for(int j=0;j<c->solgonder()->gelencizgitut.size();j++)
            {
                cizyedek=c->solgonder()->gelencizgitut[j];
                cizyedek->nodeVer1()->gidensil(cizyedek);
                remove(cizyedek);
                delete cizyedek;
            }

            for(int j=0;j<c->saggonder()->gidencizgitut.size();j++)
            {
                cizyedek=c->saggonder()->gidencizgitut[j];
                cizyedek->nodeVer2()->gelensil(cizyedek);
                remove(cizyedek);
                delete cizyedek;
            }

            for(int j=0;j<c->solgonder()->gidencizgitut.size();j++)
            {
                cizyedek=c->solgonder()->gidencizgitut[j];
                cizyedek->nodeVer2()->gelensil(cizyedek);
                remove(cizyedek);
                delete cizyedek;
            }
            compit=compTut.begin()+i;
            compTut.erase(compit);
            remove(c->saggonder());
            remove(c->solgonder());
            delete c->saggonder();
            delete c->solgonder();
            remove(c->ustbilgi);
            delete c->ustbilgi;
            p->remove(p->prop());
            delete p->prop();
            remove(c);
            delete c;
        }
    }
    p->redraw();
    tumnodetut.clear();
    tumnodetut=nodeTut;
    for(int i=0;i<compTut.size();i++)
    {
        tumnodetut.push_back(compTut[i]->saggonder());
        tumnodetut.push_back(compTut[i]->solgonder());
    }
    std::vector<eContainer *>::iterator metit;
    eContainer *eC;
    for(int i=0;i<metinTut.size();i++)
    {
        eC=metinTut[i];
        if(eC->deletesil())
        {
            metit=metinTut.begin()+i;
            remove(eC);
            metinTut.erase(metit);
            delete eC;
            fl_cursor(FL_CURSOR_DEFAULT);
        }
    }
    Fl::repeat_timeout(0.10,sil_CB,this);
}
void Cetele::tumusil_CB(Fl_Widget *p, void *userdata)
{
    Cetele *c=(Cetele *)userdata;
    c->tumusil_CB2();
}
void Cetele::tumusil_CB2()
{
    Form *p=(Form*)parent()->parent();
    clear();
    redraw();
    yedek=NULL;
    textyedek=NULL;
    p->remove(p->prop());
    p->redraw();
    cizgiTut.clear();
    nodeTut.clear();
    compTut.clear();
    metinTut.clear();
}
void Cetele::cizgiAl(Cizik *p)
{
    ciz=p;
    if(p!=NULL)
        cizgiTut.push_back(p);
}
//Cizik * Cetele::cizgiVer()
//{
//    return ciz;
//}

void Cetele::cizgiSil(Cizik *p)
{
    std::vector<Cizik *>::iterator cizit;
    for(int i=0;i<cizgiTut.size();i++)
    {
        if(p==cizgiTut[i])
        {
            cizit=cizgiTut.begin()+i;
            cizgiTut.erase(cizit);
            break;
        }
    }
}
int Cetele::getcizid()
{
    return bagid;
}
void Cetele::setcizid()
{
    bagid++;
}
void Cetele::nodeSil(Node *p)
{
    Form *pp=(Form *)parent()->parent();
    std::vector<Node *>::iterator nodit;
    for(int i=0;i<nodeTut.size();i++)
    {
        if(p==nodeTut[i])
        {
            nodit=nodeTut.begin()+i;
            nodeTut.erase(nodit);
            break;
        }
    }
    yedek==NULL;
}
void Cetele::compsil(component *p)
{
    Form *pp=(Form *)parent()->parent();
    std::vector<component *>::iterator compit;
    for(int i=0;i<compTut.size();i++)
    {
        if(p==compTut[i])
        {
            compit=compTut.begin()+i;
            compTut.erase(compit);
            break;
        }
    }
}
void Cetele::metinSil(eContainer *p)
{
    Form *pp=(Form *)parent()->parent();
    std::vector<eContainer *>::iterator metinit;
    for(int i=0;i<metinTut.size();i++)
    {
        if(p==metinTut[i])
        {
            metinit=metinTut.begin()+i;
            metinTut.erase(metinit);
            break;
        }
    }
}
void Cetele::yazdir_CB(void *userdata)
{
    Cetele *gecici=(Cetele *)userdata;
    gecici->yazdir_CB2();
}
void Cetele::yazdir_CB2()
{
    Form *p=(Form *)parent()->parent();
    if(p->tasarim()->value()==p->Code())
    {
        eContainer *mGecici;
        component *cGecici;
        Node *nGecici;
        Cizik *ciGecici;


        std::ofstream dosya;
        dosya.open("npc.txt");
        std::string baslik;
        std::string metin;
        std::string comp;
        std::string node;
        std::string cizgi;

        baslik="elektro_";
        baslik+=p->npcHarita()->value();
        baslik+=".gat";
        baslik+=",";
        baslik+=sirala(p->npcX()->value());
        baslik+=",";
        baslik+=sirala(p->npcY()->value());
        baslik+=",";
        baslik+=sirala(p->npcYon()->value());
        baslik+="\tscript\t";
        baslik+=p->npcName()->value();
        baslik+="\t";
        baslik+=sirala(p->npcId()->value());
        baslik+=",{\n\tcallfunc \"circuitstart\";\n";
        baslik+="\tcallfunc \"windowpro\",";
        baslik+=sirala(p->winWidth()->value())+","+sirala(p->winHeigth()->value())+","+sirala(p->winLeft()->value())+","+sirala(p->winTop()->value())+";\n";
        metin.clear();
        for(int i=0;i<metinTut.size();i++)
        {
            metin+="\tcallfunc \"textbox\",";
            mGecici=metinTut[i];
            metin+=sirala(mGecici->kutumetin()->x())+","+sirala(mGecici->kutumetin()->y()-60)+",";
            metin+=sirala(mGecici->kutumetin()->w())+","+sirala(mGecici->kutumetin()->h())+",";
            metin+=sirala(mGecici->cr())+","+sirala(mGecici->cg())+","+sirala(mGecici->cb())+",";
            metin+=sirala(mGecici->calpha())+","+sirala(mGecici->cdikey())+","+sirala(mGecici->cyatay())+";\n";
            std::string gecici=mGecici->kutumetin()->value();
            std::string istetme;
            for(int j=0;j<gecici.length();j++)
            {
                if(gecici[j]=='\n')
                {
                    metin+="\t\tcallfunc \"addrow\",\""+istetme+" \";\n";
                    istetme.clear();
                }
                else if(j==gecici.length()-1)
                {
                    istetme+=gecici[j];
                    metin+="\t\tcallfunc \"addrow\",\""+istetme+" \";\n";
                    istetme.clear();
                }
                else
                {
                    istetme+=gecici[j];
                }
            }
             metin+="\tcallfunc \"textbox_\";\n";
        }
        metin+="\n//                                       \tid,\titem,  x,\ty,\taci,\tst,\tsl,\tmv,\tdel,\tfl1,\ttl1,\tfl2,\ttl2\n";
        comp.clear();
        for(int i=0;i<compTut.size();i++)
        {
            cGecici=compTut[i];
            comp+="\tcallfunc \"component\",\t";
            comp+=sirala(cGecici->id())+",\t\""+cGecici->item_id()+"\",\t"+sirala(cGecici->Nitelik()->xx()->value())+",\t"+sirala(cGecici->Nitelik()->yy()->value()-16)+",\t"+sirala(cGecici->angel())+",\t";
            comp+=sirala(cGecici->status())+",\t"+sirala(cGecici->selectable())+",\t"+sirala(cGecici->movable())+",\t"+sirala(cGecici->deletable())+",\t";
            comp+=sirala(cGecici->fromLink1())+",\t"+sirala(cGecici->toLink1())+",\t"+sirala(cGecici->fromLink2())+",\t"+sirala(cGecici->toLink2())+";\n";
        }
        comp+="\n";
        node.clear();
        for(int i=0;i<nodeTut.size();i++)
        {
            nGecici=nodeTut[i];
            node+="\tcallfunc \"node\",";
            node+=sirala(nGecici->id())+", "+sirala(nGecici->nitelik()->xx()->value())+", "+sirala(nGecici->nitelik()->yy()->value())+", ";
            node+=sirala(nGecici->status())+", "+sirala(nGecici->selectable())+", "+sirala(nGecici->movable())+", ";
            node+=sirala(nGecici->deletable())+", "+sirala(nGecici->fromLink())+", "+sirala(nGecici->toLink())+";\n";
        }
        node+="\n";
        cizgi.clear();
        for(int i=0;i<cizgiTut.size();i++)
        {
            ciGecici=cizgiTut[i];
            cizgi+="\tcallfunc \"connect\",";
            cizgi+=sirala(ciGecici->cId())+", "+sirala(ciGecici->cFirst())+", "+sirala(ciGecici->cSecond())+", 1;\n";
        }
        cizgi+="\n";
        baslik+=metin+comp+node+cizgi;
        baslik+="\tcallfunc \"circuit_\";\n\tclose;\n}";
        p->kutumetin()->value(baslik.c_str());
        dosya<<baslik;
        dosya.close();
    }
    Fl::repeat_timeout(0.10,yazdir_CB,this);
}
eContainer *Cetele::textgonder()
{
    return textyedek;
}
std::string Cetele::sirala(int sayi)
{
     std::string git;
    char dizi[10]={'0','1','2','3','4','5','6','7','8','9'};
    int kalan=0;
    int sonuc=sayi/10;
    if(sonuc<1)
    {
        git=dizi[sayi];
    }
    else
    {
        sonuc=sayi/100;
        if(sonuc<1)
        {
            sonuc=sayi/10;
            git=dizi[sonuc];
            kalan=sayi%10;
            git+=dizi[kalan];
        }
        else
        {
            sonuc=sayi/100;
            git=dizi[sonuc];
            kalan=sayi%100;
            if(kalan<10)
            {
                git+=dizi[0];
                git+=dizi[kalan];
            }
            else
            {
                sonuc=kalan/10;
                git+=dizi[sonuc];
                kalan=kalan%10;
                git+=dizi[kalan];
            }

        }
    }
    return git;
}
